﻿using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Micro.Extensions.Utilities
{
    public static class StringExtension
    {
        #region 命名
        /// <summary>
        /// 驼峰命名转下划线命名
        /// </summary>
        /// <param name="camelName"></param>
        /// <returns></returns>
        public static string CamelNameToUnderLine(this string camelName)
        {
            var chars = camelName.ToArray();
            var sb = new StringBuilder();
            foreach (var ch in chars)
            {
                if (char.IsUpper(ch))
                {
                    sb.Append('_').Append(char.ToLower(ch));
                }
                else
                {
                    sb.Append(ch);
                }
            }
            return sb.ToString().Trim('_');
        }
        /// <summary>
        /// 下划线命名转驼峰
        /// </summary>
        /// <param name="underLine"></param>
        /// <returns></returns>
        public static string UnderLineToCamelName(this string underLine)
        {
            var chars = underLine.ToArray();
            var sb = new StringBuilder();

            var nextUpper = false;
            sb.Append(char.ToUpper(chars[0]));

            for (var i = 1; i < chars.Length; i++)
            {
                var ch = chars[i];
                if (ch == '_')
                {
                    nextUpper = true;
                    continue;
                }

                if (nextUpper)
                {
                    sb.Append(char.ToUpper(ch));
                    nextUpper = false;
                }
                else
                {
                    sb.Append(ch);
                }
            }
            return sb.ToString().Trim('_');
        }
        /// <summary>
        /// 首字母小写命名
        /// </summary>
        /// <param name="camelName"></param>
        /// <returns></returns>
        public static string LowerCamel(this string camelName)
        {
            var chars = camelName.ToArray();
            var sb = new StringBuilder();
            sb.Append(char.ToLower(chars[0]));
            for (var i = 1; i < chars.Length; i++)
            {
                sb.Append(chars[i]);
            }

            return sb.ToString();
        }
        #endregion

        #region 加密解密
        /// <summary>
        /// MD5 加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToMd5(this string str)
        {
            return Md5Util.Md5(str);
        }


        ///// <summary>
        ///// AES 加密
        ///// </summary>
        ///// <param name="str"></param>
        ///// <returns></returns>
        //public static string ToAES(this string str, string key, string vi)
        //{
        //    return AesUtil.EncryptToStr(str, key, vi);
        //}

        ///// <summary>
        ///// AES 加密
        ///// </summary>
        ///// <param name="str"></param>
        ///// <returns></returns>
        //public static string ToAES(this string str, byte[] key, byte[] vi)
        //{
        //    return AesUtil.EncryptToStr(str, key, vi);
        //}


        ///// <summary>
        ///// AES 加密
        ///// </summary>
        ///// <param name="str"></param>
        ///// <returns></returns>
        //public static string FromAES(this string str, string key, string vi)
        //{
        //    return AesUtil.Decrypt(str, key, vi);
        //}

        ///// <summary>
        ///// AES 加密
        ///// </summary>
        ///// <param name="str"></param>
        ///// <returns></returns>
        //public static string FromAES(this string str, byte[] key, byte[] vi)
        //{
        //    return AesUtil.Decrypt(str, key, vi);
        //}
        #endregion

        #region 正则匹配
        private static readonly Regex Email = new Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$", RegexOptions.Compiled);
        /// <summary>
        /// 是否邮箱
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsEmail(this string str)
        {
            if (str.IsNullOrEmpty())
            {
                return false;
            }
            return Email.IsMatch(str);
        }
        #endregion

        #region 扩展
        /// <summary>
        /// IsNullOrEmpty 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }
        #endregion

    }
}
